-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday9 - part1.py
More file actions
45 lines (35 loc) · 937 Bytes
/
day9 - part1.py
File metadata and controls
45 lines (35 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
file = open("input.txt")
lines = file.read().splitlines()
def calculate_tail(t, h, m):
xd = abs(h[0] - t[0])
yd = abs(h[1] - t[1])
if xd < 2 and yd < 2:
return t
if xd == 2 and t[1] == h[1]:
return [t[0] + m[0], t[1]]
if yd == 2 and t[0] == h[0]:
return [t[0], t[1] + m[1]]
return [h[0] + m[0] * -1, h[1] + m[1] * -1]
head = [0, 0]
tail = head.copy()
tail_positions = set()
tail_positions.add(tuple(tail))
move = (0, 0)
for line in lines:
side, step = line.split()
step = int(step)
match side:
case "R":
move = (1, 0)
case "U":
move = (0, 1)
case "L":
move = (-1, 0)
case "D":
move = (0, -1)
for _ in range(step):
head[0] += move[0]
head[1] += move[1]
tail = calculate_tail(tail, head, move)
tail_positions.add(tuple(tail))
print(len(tail_positions))